package cn.demoncat.util.hazelcast.config;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.hazelcast.config.Config;
import com.hazelcast.config.ListConfig;
import com.hazelcast.config.MapConfig;
import com.hazelcast.config.QueueConfig;
import com.hazelcast.config.SetConfig;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;

import cn.demoncat.util.lang.ArrayUtil;
import cn.demoncat.util.lang.StringUtil;
import cn.demoncat.util.lang.constant.StatusConstant.Common;
import cn.demoncat.util.lang.constant.StringConstant;

/**
 * Hazelcast 服务端配置
 * 
 * @author 延晓磊
 *
 * @since 2020年1月2日
 */
@Configuration
public class HzServerConfig {

	@Resource
	private HzProperties hzProperties;

	/**
	 * 配置对象
	 */
	@ConditionalOnMissingBean(name = { "hazelcastServerConfig", "hazelcastConfig" })
	@Bean
	public Config hazelcastConfig() {
		// 创建配置
		Config config = new Config();
		// 实例名称：根据Name获取实例
		config.setInstanceName(hzProperties.getInstance());
		// 集群分组：发现相同组名的节点
		config.getGroupConfig().setName(hzProperties.getGroup());
		// 网络配置：默认组播协议，端口5700+100
		config.getNetworkConfig().setPort(hzProperties.getPort());
		
		// 数据配置
		String[] datas;
		String name;
		int backup;
		boolean readBackup;
		for (String cfg : hzProperties.getMaps()) {
			if (StringUtils.isNotBlank(cfg)) {
				datas = StringUtils.split(cfg, StringConstant.COLON);
				name = ArrayUtil.get(datas, 0) + StringConstant.STAR;
				backup = Integer.parseInt(StringUtil.toDefault(ArrayUtil.get(datas, 1), StringConstant.N1));
				readBackup = !Common.NO.equals(ArrayUtil.get(datas, 2));
				// IMap
				config.addMapConfig(new MapConfig().setName(name).setBackupCount(backup).setReadBackupData(readBackup));
			}
		}
		for (String cfg : hzProperties.getQueues()) {
			if (StringUtils.isNotBlank(cfg)) {
				datas = StringUtils.split(cfg, StringConstant.COLON);
				name = ArrayUtil.get(datas, 0) + StringConstant.STAR;
				backup = Integer.parseInt(StringUtil.toDefault(ArrayUtil.get(datas, 1), StringConstant.N1));
				readBackup = !Common.NO.equals(ArrayUtil.get(datas, 2));
				// IQueue
				config.addQueueConfig(new QueueConfig().setName(name).setBackupCount(backup));
			}
		}
		for (String cfg : hzProperties.getLists()) {
			if (StringUtils.isNotBlank(cfg)) {
				datas = StringUtils.split(cfg, StringConstant.COLON);
				name = ArrayUtil.get(datas, 0) + StringConstant.STAR;
				backup = Integer.parseInt(StringUtil.toDefault(ArrayUtil.get(datas, 1), StringConstant.N1));
				// IList
				config.addListConfig(new ListConfig().setName(name).setBackupCount(backup));
			}
		}
		for (String cfg : hzProperties.getSets()) {
			if (StringUtils.isNotBlank(cfg)) {
				datas = StringUtils.split(cfg, StringConstant.COLON);
				name = ArrayUtil.get(datas, 0) + StringConstant.STAR;
				backup = Integer.parseInt(StringUtil.toDefault(ArrayUtil.get(datas, 1), StringConstant.N1));
				// ISet
				config.addSetConfig(new SetConfig().setName(name).setBackupCount(backup));
			}
		}
		return config;
	}

	/**
	 * Hazelcast 实例
	 */
	@Bean
	public HazelcastInstance hzInstance(Config hazelcastConfig) {
		// 创建Hazelcast服务端：处理数据，维护节点信息
		return Hazelcast.newHazelcastInstance(hazelcastConfig);
	}
}
